import { Test } from './utils/Classes/Test';
/*
 * @Date: 2023-01-06 10:05:06
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-04-11 11:45:50
 * @FilePath: \A19\backend\src\index.ts
 */
/*
 * @Date: 2023-01-06 10:05:06
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-12 19:27:22
 * @FilePath: \A19\backend\src\index.ts
 */
/*
 * @Date: 2023-01-06 10:05:06
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-12 18:13:20
 * @FilePath: \A19\backend\src\index.ts
 */
import { Gender } from './utils/Gender';
import { Global } from './Global/index';
import * as path from "path";
import { HttpServer, WsServer } from "tsrpc";
import { serviceProto } from './shared/protocols/serviceProto';
import { isLogined, isLoginedWs } from './flowNodes/isLogined';
import { onStudentOffline } from './flowNodes/studentOffline';
import { onTeacherOffline } from './flowNodes/teacherOffline';
import { ObjectId } from 'mongodb';
import isGet from './flowNodes/isGet';
import { serverPort, wsserverPOrt } from './dev.json'
import { createCode } from './utils/createCode';
import { needRole, needRoleWs } from './flowNodes/needRole';
import { users } from './shared/types/user';
import { messageImplement } from './utils/messageImplement';
import { gameMsgImplements } from './utils/msgsForGame/gameMsgImplements';
import encry from './flowNodes/encry';
// Create the Server
export const Wsserver = new WsServer(serviceProto, {
    port: wsserverPOrt,
    // Remove this to use binary mode (remove from the client too)
    json: false,
    logLevel:'warn',
    heartbeatWaitTime:100000
});
export const server = new HttpServer(serviceProto, {
    port: serverPort,
    json: false,
    logLevel: 'warn',
})



// Initialize before server start
async function init() {
    await server.autoImplementApi(path.resolve(__dirname, 'api'));

    //filter
    await encry(server,Wsserver)
    await isGet(server)
    await isLogined(server)
    await needRole(server)
    // isLoginedWs(Wsserver)
    // needRoleWs(Wsserver)
    onStudentOffline(Wsserver)
    onTeacherOffline(Wsserver)

    messageImplement(Wsserver)
    gameMsgImplements(Wsserver)
    // TODO
    // Prepare something... (e.g. connect the db)


    await Global.initDb()
    const tests = await Global.collection('tests').find({}).toArray()
    tests.forEach(test => {
        if (test.endTime > new Date()) {
            new Test(test)
        }
    })
    console.log(Test.tests.length + " tests remained");
    
};

// Entry function
async function main() {
    await init();
    await server.start();
    await Wsserver.start();

    // const ret = await server.callApi('Login/Regist', {
    //     portrait: {
    //         fileType: "image/png",
    //         fileDate: new Uint8Array()
    //     },
    //     role: 'student',
    //     name: 'Natsuki',
    //     gender: Gender.female,
    //     email: 'Nova@asdf.asdf',
    //     password: 'natsuki'
    // })
    // console.log(ret);

    // createCode()
    // const ret = await server.callApi('Login/TestEmail',{
    //     email:'1193875164@qq.com'
    // })
    // console.log(ret);

    // const ret = await server.callApi('Login/FindPass', {
    //     email: 'zireael@asdf.asdf',
    //     password: 'zireael'
    // })
    // console.log(ret);

    // Global.collection('homework').insertOne({
    //     course: '63c0c09ae58ff204b7dcceaa',
    //     startTime: new Date(),
    //     endTime: new Date(),
    //     name: "作业一"
    // })

    // const ret = await server.callApi('Get/GetSchools',{__token:'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzYjc4NGI4MmM0MzNlNTBhYWViZjg4YSIsInJvbGUiOiJzdHVkZW50IiwiaWF0IjoxNjczNjA2MTIzLCJleHAiOjE2NzQwMzgxMjN9.1A8Pj7qjmsNDfUs_29KDhzxsjPdx04y7umua7WzlYCVBd_zJDc54mrawN8T1MaXuIjdhDJbSi81CPFi3MCyoRA'})
    // ret.res?.schools.forEach(i =>{
    //     console.log(i);
    // })



}
main();

declare module 'tsrpc' {
    export interface ApiCall {
        currentUser: users
    }
    export interface MsgCall {
        currentUser: users
    }
}